package com.youyi.yyscreencutlibrary.Track;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;

import com.lmiot.xyclick.Util.LkxLog;
import com.youyi.yyscreencutlibrary.SDK.YYScreenCutSDK;

import java.nio.ByteBuffer;

public class YYTrackSDK {
    private static final String TAG = "YYTrackSDK";
    public static Intent data;
    public static boolean hasPermission = false;
    public static boolean hasStart = false;
    public static ImageReader imageReader;
    private static volatile boolean mHasCheckAllScreen;
    private static volatile boolean mIsAllScreenDevice;
    public static OnDestroyRecordListener mOnDestroyRecordListener;
    public static OnStrartRecordListener mOnStrartRecordListener;
    private static final YYTrackSDK ourInstance = new YYTrackSDK();
    public static MediaProjection projection;
    public static MediaProjectionManager projectionManager;
    public static int resultCode;
    public static Bitmap targetBitmap;
    private Context mContext;
    private boolean mIsFirstCut;
    public OnCallDestroyListener mOnCallDestroyListener;

    public interface OnCallDestroyListener {
        void result(boolean z);
    }

    public interface OnDestroyRecordListener {
        void result(boolean z, String str);
    }

    public interface OnStrartRecordListener {
        void result(boolean z, String str);
    }

    public static YYTrackSDK getInstance() {
        return ourInstance;
    }

    private YYTrackSDK() {
    }

    public void startRecording(Context context, OnStrartRecordListener onStrartRecordListener) {
        this.mContext = context;
        mOnStrartRecordListener = onStrartRecordListener;
        hasStart = false;
        YYScreenCutSDK.mIsFinish = false;
        if (!isRecording()) {
            Intent intent = null;
            if (Build.VERSION.SDK_INT >= 21) {
                intent = new Intent(context, YYTrackLibActivity.class);
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    public void destroyRecording(OnDestroyRecordListener onDestroyRecordListener) {
        OnCallDestroyListener onCallDestroyListener = this.mOnCallDestroyListener;
        if (onCallDestroyListener != null) {
            onCallDestroyListener.result(true);
        }
        hasStart = false;
        mOnDestroyRecordListener = onDestroyRecordListener;
    }

    public boolean isRecording() {
        return imageReader != null;
    }

    public Bitmap getBitMap() {
        LkxLog.d("修复后的方法");
        Image image;
        Image image1;
        boolean b =true;
        try {
            try {
                this.mIsFirstCut = true;
                if (Build.VERSION.SDK_INT >= 29 && (image1 = YYTrackSDK.imageReader.acquireLatestImage()) != null) {
                    image1.close();
                }
            } catch (java.lang.Exception e0) {
                e0.printStackTrace();
            }
            Bitmap uBitmap = null;
            int i = 21;
            try {
                if ((image = YYTrackSDK.imageReader.acquireLatestImage()) == null) {
                    for (int i1 = 0; i1 < 20 && (image = YYTrackSDK.imageReader.acquireLatestImage()) == null; i1 = i1 + 1) {
                        Thread.sleep(100);
                    }
                }
                if (image != null && image.getPlanes().length > 0) {
                    uBitmap = this.getScreenBitmap(image);
                }
                if (image != null) {
                    image.close();
                }
                if (Build.VERSION.SDK_INT < 29 && (YYTrackSDK.projection != null && Build.VERSION.SDK_INT >= i)) {
                    YYTrackSDK.projection.stop();
                }
                return uBitmap;
            } catch (java.lang.Exception e6) {
                e6.printStackTrace();
            }
            if (YYScreenCutSDK.getYunPhoneFlag(this.mContext)) {
                b = false;
            }
            YYScreenCutSDK.setYunPhoneFlag(mContext, b);
            if (Build.VERSION.SDK_INT < 29 && (YYTrackSDK.projection != null && Build.VERSION.SDK_INT >= i)) {
                YYTrackSDK.projection.stop();
            }
            return uBitmap;
        } catch (java.lang.Exception e6) {
            e6.printStackTrace();
            return null;
        }
    }

    public Bitmap getBitMap00() {
        try {
            Image acquireLatestImage = imageReader.acquireLatestImage();
            if (acquireLatestImage == null) {
                return null;
            }
            int width = acquireLatestImage.getWidth();
            int height = acquireLatestImage.getHeight();
            Image.Plane[] planes = acquireLatestImage.getPlanes();
            ByteBuffer buffer = planes[0].getBuffer();
            int pixelStride = planes[0].getPixelStride();
            Bitmap createBitmap = Bitmap.createBitmap(((planes[0].getRowStride() - (pixelStride * width)) / pixelStride) + width, height, Bitmap.Config.ARGB_8888);
            createBitmap.copyPixelsFromBuffer(buffer);
            acquireLatestImage.close();
            reflect(imageReader);
            if (isScreenOriatationPortrait(this.mContext)) {
                int width2 = createBitmap.getWidth() * createBitmap.getHeight();
                int[] iArr = new int[width2];
                createBitmap.getPixels(iArr, 0, createBitmap.getWidth(), 0, 0, createBitmap.getWidth(), 1);
                int i = 0;
                while (true) {
                    if (i >= width2) {
                        i = 0;
                        break;
                    } else if (iArr[i] != 0) {
                        break;
                    } else {
                        i++;
                    }
                }
                try {
                    return Bitmap.createBitmap(createBitmap, i, 0, width - (i * 2), height);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                int width3 = createBitmap.getWidth() * createBitmap.getHeight();
                int[] iArr2 = new int[width3];
                createBitmap.getPixels(iArr2, 0, 1, 0, 0, 1, createBitmap.getHeight());
                int i2 = 0;
                while (true) {
                    if (i2 >= width3) {
                        i2 = 0;
                        break;
                    } else if (iArr2[i2] != 0) {
                        break;
                    } else {
                        i2++;
                    }
                }
                int i3 = height - (i2 * 2);
                try {
                    createBitmap = Bitmap.createBitmap(createBitmap, 0, i2, width, i3);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                if (getInstance().isAllScreenDevice(this.mContext)) {
                    return Bitmap.createBitmap(createBitmap, getStatusBarHeight(this.mContext), 0, width - getStatusBarHeight(this.mContext), i3);
                }
                return createBitmap;
            }
        } catch (Exception e3) {
            e3.printStackTrace();
            return null;
        }
        return null;
    }

    public void setOnCallDestroyListener(OnCallDestroyListener onCallDestroyListener) {
        this.mOnCallDestroyListener = onCallDestroyListener;
    }

    private Bitmap getScreenBitmap(Image image) {
        int width = image.getWidth();
        int height = image.getHeight();
        Image.Plane[] planes = image.getPlanes();
        ByteBuffer buffer = planes[0].getBuffer();
        int pixelStride = planes[0].getPixelStride();
        Bitmap createBitmap = Bitmap.createBitmap(((planes[0].getRowStride() - (pixelStride * width)) / pixelStride) + width, height, Bitmap.Config.ARGB_8888);
        createBitmap.copyPixelsFromBuffer(buffer);
        Bitmap createBitmap2 = Bitmap.createBitmap(createBitmap, 0, 0, width, height);
        image.close();
        if (isScreenOriatationPortrait(this.mContext)) {
            int width2 = createBitmap2.getWidth() * createBitmap2.getHeight();
            int[] iArr = new int[width2];
            createBitmap2.getPixels(iArr, 0, createBitmap2.getWidth(), 0, 0, createBitmap2.getWidth(), 1);
            int i = 0;
            while (true) {
                if (i >= width2) {
                    i = 0;
                    break;
                } else if (iArr[i] != 0) {
                    break;
                } else {
                    i++;
                }
            }
            try {
                return Bitmap.createBitmap(createBitmap2, i, 0, width - (i * 2), height);
            } catch (Exception e) {
                e.printStackTrace();
                return createBitmap2;
            }
        } else {
            int width3 = createBitmap2.getWidth() * createBitmap2.getHeight();
            int[] iArr2 = new int[width3];
            createBitmap2.getPixels(iArr2, 0, 1, 0, 0, 1, createBitmap2.getHeight());
            int i2 = 0;
            while (true) {
                if (i2 >= width3) {
                    i2 = 0;
                    break;
                } else if (iArr2[i2] != 0) {
                    break;
                } else {
                    i2++;
                }
            }
            int i3 = height - (i2 * 2);
            try {
                createBitmap2 = Bitmap.createBitmap(createBitmap2, 0, i2, width, i3);
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            return YYScreenCutSDK.getInstance().isAllScreenDevice(this.mContext) ? Bitmap.createBitmap(createBitmap2, YYScreenCutSDK.StarBarHeight, 0, width - YYScreenCutSDK.StarBarHeight, i3) : createBitmap2;
        }
    }

    public boolean isAllScreenDevice(Context context) {
        float f;
        int i;
        if (mHasCheckAllScreen) {
            return mIsAllScreenDevice;
        }
        mHasCheckAllScreen = true;
        mIsAllScreenDevice = false;
        if (Build.VERSION.SDK_INT < 21) {
            return false;
        }
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            Display defaultDisplay = windowManager.getDefaultDisplay();
            Point point = new Point();
            defaultDisplay.getRealSize(point);
            if (point.x < point.y) {
                f = (float) point.x;
                i = point.y;
            } else {
                f = (float) point.y;
                i = point.x;
            }
            if (((float) i) / f >= 1.97f) {
                mIsAllScreenDevice = true;
            }
        }
        return mIsAllScreenDevice;
    }

    public void reflect(ImageReader imageReader2) {
        try {
            imageReader2.getClass().getDeclaredMethod("discardFreeBuffers", new Class[0]).invoke(imageReader2, new Object[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isScreenOriatationPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == 1;
    }

    public static int getStatusBarHeight(Context context) {
        int identifier = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            return context.getResources().getDimensionPixelSize(identifier);
        }
        return (int) TypedValue.applyDimension(1, (float) 24, Resources.getSystem().getDisplayMetrics());
    }
}
